package controller.material;

import java.sql.SQLException;

import service.CoffeeService;
import to.JobTO;
import util.CoffeeUtil;

public class NewCropSlio extends Thread {
	private CoffeeService service;

	public NewCropSlio(CoffeeService service) {
		this.service = service;
	}

	@Override
	public void run() {
		while (true) {
			try {
				int saengdooSilo = service.selectSiloBeansTotalWeight("생두silo");
				if ((saengdooSilo < CoffeeUtil.NEW_CROP_SILO_WEIGHT) && (service.selectMaterialTotalWeight("원두") > 500)) {// 생두사일로
					// 총중량 -500
					String sTime = CoffeeUtil.getNow();
					CoffeeUtil.sleep(5000);
					service.updateMaterialTotalWeight("원두", -500);
					String eTime = CoffeeUtil.getNow();
					// 작업 내용 등록
					JobTO jto = new JobTO("원두출고", sTime, eTime);
					jto.setWorkLoad(-500);
					service.insertJobInfo(jto);

					int fraction = (int) ((Math.random() * 5) + 2);
					// 500-(500*(fraction*0.01)); 생두사일로에 이만큼 추가
					String sTime2 = CoffeeUtil.getNow();
					CoffeeUtil.sleep(CoffeeUtil.MATERIAL_INPUT_TIME);
					service.updateSiloStatus("생두silo",(int) (500 - (500 * (fraction * 0.01))));
					String eTime2 = CoffeeUtil.getNow();
					// 작업 내용 등록
					JobTO jto2 = new JobTO("생두입고", sTime2, eTime2);
					jto2.setWorkLoad((int) (500 - (500 * (fraction * 0.01))));
					service.insertJobInfo(jto2);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
